import Particle from './particle';
import Expression from '../compute/expression';

const FRACTION=(num,mul=1)=>{
    let n=num*mul;
    return n-Math.floor(n);
};

export default class ParticlePath extends Particle{
    constructor(values={}){
        super(values);
        this.props.add({
            tMin:values.tMin||0,
            tMax:values.tMax||1,
            uMin:values.uMin||0,
            uMax:values.uMax||1,
            thickness:values.thickness||0.1,
            xScale:values.xScale||2,
            zScale:values.zScale||2,
        });
    }
    get setPath(){
        return (xStr,yStr,zStr,pt,pu)=>{
            this.xExpression.input(xStr,pt,pu);
            this.yExpression.input(yStr,pt,pu);
            this.zExpression.input(zStr,pt,pu);
        };
    }
    initPropCallback(){
        super.initPropCallback();
        this.props.onMultiKeyChange(()=>{
            this.dt=this.props.tMax.value-this.props.tMin.value;
        },'tMin','tMax');
        this.props.onMultiKeyChange(()=>{
            this.du=this.props.uMax.value-this.props.uMin.value;
        },'uMin','uMax');
    }
    initVariable(){
        super.initVariable();
        this.xExpression=new Expression.E();
        this.yExpression=new Expression.E();
        this.zExpression=new Expression.E();
        this.xExpression.input('t','t');
        this.yExpression.input('0','t');
        this.zExpression.input('u','u');
        this.dt=this.props.tMax.value-this.props.tMin.value;
        this.du=this.props.uMax.value-this.props.uMin.value;
    }
    setStartVertSpeed(rand,i,t){
        let pt=FRACTION(rand,1e4)*this.dt+this.props.tMin.value;
        let pu=FRACTION(rand,1e5)*this.du+this.props.uMin.value;
        this.verts[i]=this.xExpression.output(pt,pu)-this.props.thickness.value+this.props.thickness.value*2*FRACTION(rand,1e6);
        this.verts[i+1]=this.yExpression.output(pt,pu);
        this.verts[i+2]=this.zExpression.output(pt,pu)-this.props.thickness.value+this.props.thickness.value*2*FRACTION(rand,1e7);

        this.speeds[i]=this.verts[i]*this.props.xScale.value-this.verts[i];
        this.speeds[i+1]=this.props.speedMin.value+(this.props.speedMax.value-this.props.speedMin.value)*FRACTION(rand,1e8);
        this.speeds[i+2]=this.verts[i+2]*this.props.zScale.value-this.verts[i+2];
    }
}